package cz.muni.fi.pa165.mushroom.website.servicesImpl;


import cz.muni.fi.pa165.mushroom.website.dao.VisitDao;
import cz.muni.fi.pa165.mushroom.website.dto.ForestDto;
import cz.muni.fi.pa165.mushroom.website.dto.GathererDto;
import cz.muni.fi.pa165.mushroom.website.dto.VisitDto;
import cz.muni.fi.pa165.mushroom.website.entities.Forest;
import cz.muni.fi.pa165.mushroom.website.entities.Gatherer;
import cz.muni.fi.pa165.mushroom.website.entities.Visit;
import cz.muni.fi.pa165.mushroom.website.services.VisitService;
import cz.muni.fi.pa165.mushroom.website.transformerAdapter.Transformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresRoles;

/**
 * @author: Roman Jakubco
 */

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class VisitServiceImpl implements VisitService {

    @Autowired
    private VisitDao visitDao;

    public VisitDao getVisitDao() {
        return visitDao;
    }

    public void setVisitDao(VisitDao visitDao) {
        this.visitDao = visitDao;
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public void createVisit(VisitDto visitDto) {
        visitDao.createVisit(Transformer.transformVisitDtoOnVisit(visitDto));
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public void updateVisit(VisitDto visitDto) {
        visitDao.updateVisit(Transformer.transformVisitDtoOnVisit(visitDto));
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public void deleteVisit(VisitDto visitDto) {
        visitDao.deleteVisit(Transformer.transformVisitDtoOnVisit(visitDto));
    }

    @Override
    @RequiresRoles("ROLE_USER")
    @Transactional
    public VisitDto findVisit(Long id) {
        return Transformer.transformVisitOnVisitDto(visitDao.findVisit(id));
    }

    @Override
    @Transactional
    public List<VisitDto> listVisits() {
        List<VisitDto> dtos = new ArrayList<>();
        for(Visit visit : visitDao.listVisits()){
            dtos.add(Transformer.transformVisitOnVisitDto(visit));
        }

        return dtos;
    }

    @Override
    @Transactional
    public List<VisitDto> listVisitsByForest(ForestDto forest) {
        List<VisitDto> dtos = new ArrayList<>();
        Forest f = Transformer.transformForestDtoOnForest(forest);
        for(Visit visit : visitDao.listVisitsByForest(f)){
            dtos.add(Transformer.transformVisitOnVisitDto(visit));
        }

        return dtos;
    }

    @Override
    @Transactional
    public List<VisitDto> listVisitsByGatherer(GathererDto gatherer) {
        List<VisitDto> dtos = new ArrayList<>();
        Gatherer g = Transformer.transformGathererDtoOnGatherer(gatherer);        
        for(Visit visit : visitDao.listVisitsByGatherer(g)){
            dtos.add(Transformer.transformVisitOnVisitDto(visit));
        }

        return dtos;
    }
    
    
    

}
